package com.xuecheng.manage_media_process.mq;

import com.alibaba.fastjson.JSONObject;
import com.xuecheng.framework.domain.media.MediaFile;
import com.xuecheng.framework.domain.media.MediaFileProcess_m3u8;
import com.xuecheng.framework.utils.HlsVideoUtil;
import com.xuecheng.framework.utils.Mp4VideoUtil;
import com.xuecheng.manage_media_process.dao.MediaFileRepository;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@Component
public class MediaProcessTask {
    @Value("${xc-service-manage-media.ffmpeg-path}")
    private String ffmpegPath;

    @Value("${xc-service-manage-media.video-location}")
    private String baseFolder;

    @Autowired
    private MediaFileRepository mediaFileRepository;
    
    @RabbitListener(queues="${xc-service-manage-media.mq.queue-media-video-processor}",
    containerFactory = "multiRabbitListenerContainer")
    public void receiveMediaProcessTask(String msg){
        Map map = JSONObject.parseObject(msg, Map.class);
        String mediaId = (String) map.get("mediaId");

        Optional<MediaFile> mediaFileOptional = mediaFileRepository.findById(mediaId);
        if(!mediaFileOptional.isPresent()){
            return;
        }

        MediaFile mediaFile = mediaFileOptional.get();
        String fileType = mediaFile.getFileType();
        String fileFolder = baseFolder + mediaFile.getFilePath();
        String mp4Name = mediaFile.getFileId() + ".mp4";
        String srcFile = fileFolder + mediaFile.getFileName();
        if(!fileType.equals("avi")){
            mediaFile.setProcessStatus("303004");
            mediaFileRepository.save(mediaFile);
            return;
        }else{
            //生成mp4文件
            //处理中
            mediaFile.setProcessStatus("303001");
            mediaFileRepository.save(mediaFile);

            //处理视频文件
            Mp4VideoUtil mp4VideoUtil = new Mp4VideoUtil(ffmpegPath, srcFile, mp4Name,fileFolder);
            String result = mp4VideoUtil.generateMp4();
            if(!"success".equals(result)){
                //处理失败
                mediaFile.setProcessStatus("303003");
                MediaFileProcess_m3u8 mediaFileProcess_m3u8 = new MediaFileProcess_m3u8();
                mediaFileProcess_m3u8.setErrormsg(result);
                mediaFile.setMediaFileProcess_m3u8(mediaFileProcess_m3u8);
                mediaFileRepository.save(mediaFile);
                return;
            }
        }

        //生成m3u8文件
        String m3u8Name = mediaFile.getFileId() + ".m3u8";
        String m3u8Folder = fileFolder + "hls/";
        HlsVideoUtil hlsVideoUtil = new HlsVideoUtil(ffmpegPath, fileFolder + mp4Name, m3u8Name, m3u8Folder);
        String hlsResult = hlsVideoUtil.generateM3u8();
        if(!"success".equals(hlsResult)){
            //处理失败
            mediaFile.setProcessStatus("303003");
            MediaFileProcess_m3u8 mediaFileProcess_m3u8 = new MediaFileProcess_m3u8();
            mediaFileProcess_m3u8.setErrormsg(hlsResult);
            mediaFile.setMediaFileProcess_m3u8(mediaFileProcess_m3u8);
            mediaFileRepository.save(mediaFile);
            return;
        }
        List<String> tsList = hlsVideoUtil.get_ts_list();
        MediaFileProcess_m3u8 mediaFileProcess_m3u8 = new MediaFileProcess_m3u8();
        mediaFileProcess_m3u8.setTslist(tsList);
        mediaFile.setMediaFileProcess_m3u8(mediaFileProcess_m3u8);
        mediaFile.setProcessStatus("303002");

        String fileUrl = mediaFile.getFilePath() + "hls/" + m3u8Name;
        mediaFile.setFileUrl(fileUrl);
        mediaFileRepository.save(mediaFile);
    }
}
